﻿using RPAVIEW.util;
using RPAVIEW.views.OtherPages;
using SUNRPA.CONTROLS.IMPL.domain.flowItemParam;
using SUNRPA.CONTROLS.IMPL.util;
using System;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using CImage = System.Windows.Controls.Image;
using DPoint = System.Drawing.Point;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using OpenCvSharp;
using SUNRPA.CONTROLS.IMPL.domain.constant;
using System.Collections.Generic;

namespace RPAVIEW.views.EditPage.flows.ClickImgN {
    /// <summary>
    /// Input.xaml 的交互逻辑
    /// </summary>
    public partial class Input:BaseParamViewAction{
        public Input() {
            InitializeComponent();
        }

        public override void UpdateSource() {
            this.searchAreaBox.GetBindingExpression(ComboBox.SelectedIndexProperty).UpdateSource();
            this.buttonTypeBox.GetBindingExpression(ComboBox.SelectedIndexProperty).UpdateSource();
            this.clickTypeBox.GetBindingExpression(ComboBox.SelectedIndexProperty).UpdateSource();
            this.selectedPic.GetBindingExpression(CImage.SourceProperty).UpdateSource();
        }

        private void SelectFromFile(object sender, System.Windows.RoutedEventArgs e) {
            string path = string.Empty;
            Microsoft.Win32.OpenFileDialog openFileDlg = FileUtil.OpenFileSelectDialog(".jpg", "图片 (*.jpg,*.png)|*.png;*.jpg", path);
            //允许多选
            //openFileDlg.Multiselect = true;
            //openFileDlg.Filter = "All files (*.*)|*.*";
 
            // 调用ShowDialog方法显示对话框
            openFileDlg.ShowDialog();
            // 显示名称
            path = openFileDlg.FileName;
            if(!File.Exists(path)) {
                return;
            }
            string fileName = Path.GetFileName(path);
            string extention = Path.GetExtension(path);
            if("jpg".Equals(extention) || "png".Equals(extention) || "ico".Equals(extention)) { 
                MessageBox.Show("文件只能是jpg、png、ico的图片文件");
                return;
            }
            string newFile = ParamUtil.GetAppGlobleVar<Dictionary<string, string>>(DefaultContents.USER_DIRS)["picLibrary"] + Path.DirectorySeparatorChar 
                + fileName.Substring(0, fileName.LastIndexOf(".")) + "." + extention;
            ClickImgNParam param = WindowUtil.GetWindow<FlowEditWindow>().DataContext as ClickImgNParam;
            File.Copy(path, newFile, true);
            param.ImagePath = Path.GetFullPath(newFile);
        }

        private void CutFromDesk(object sender, System.Windows.RoutedEventArgs e) {
            WindowUtil.MinSizeWindow(WindowUtil.GetWindow<MainWindow>());
            Bitmap bmp = ImageUtil.TakeScreenShoot();
            CutterWindow cutter = new CutterWindow(bmp);
            cutter.Background = new ImageBrush(ConverterUtil.Bitmap2ImageSource(bmp));
            cutter.ShowInTaskbar = false;
            cutter.Width = bmp.Width;
            cutter.Height = bmp.Height;
            cutter.Left = 0;
            cutter.Top = 0;
            cutter.Show();
            User32Utils.SetForegroundWindow(ElementUtil.getWindowPtr(cutter));
        }

        private void ValidatePos(object sender, RoutedEventArgs e) {
            MainWindow mainWindow = WindowUtil.GetWindow<MainWindow>();
            ClickImgNParam param = WindowUtil.GetWindow<FlowEditWindow>().DataContext as ClickImgNParam;
            string imgPath = param.ImagePath;
            WindowUtil.MinSizeWindow(mainWindow);
            Bitmap currentDeskTop = ImageUtil.TakeScreenShoot();
            Bitmap searchPic = new Bitmap(imgPath);
            DPoint tp = ImageUtil.GetPicturePos(currentDeskTop, searchPic, 1);
            if(tp.X == -1 || tp.Y == -1) { 
                MessageBox.Show("没有找到对应图片");
                WindowUtil.RecoverWindow(mainWindow);
                mainWindow.Activate();
                User32Utils.SetForegroundWindow(ElementUtil.getWindowPtr(mainWindow));
                return;
            }
            int h = searchPic.Height;
            int w = searchPic.Width;
            ImageUtil.HighLightArea(currentDeskTop, tp, w, h);
            Cv2.ImShow("校验结果", ImageUtil.BitMap2Mat(currentDeskTop));
        }
    }
}
